package com.xuanwu;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.View;

public class ImageUtil {
	static {
		System.loadLibrary("JpegUtil");
	}

	/**
	 * @param inputfilename
	 *            需要压缩的图片的当前路径
	 * @param outputfilename
	 *            保存的压缩后图片的路径
	 * @param orientation
	 *            旋转角度
	 * @param compress_level
	 *            压缩等级，目前只有0,register_1,2 0为原图压缩，1为1/2尺寸压缩，2为1/4尺寸压缩
	 */
	private native void recompressJPGFromFile(String inputfilename,
			String outputfilename, int orientation, int compress_level,
			int quality);

	/**
	 * @param bitmap
	 *            需要压缩的位图
	 * @param outputfilename
	 *            保存的压缩后图片的路径
	 * @param orientation
	 *            旋转角度
	 * @param compress_level
	 *            压缩等级，目前只有0,register_1 0为原图压缩，1为1/2尺寸压缩
	 */
	private native void recompressJPGFromBitmap(Bitmap bitmap,
			String outputfilename, int orientation, int compress_level);

	public int compressJPGFromFile(String inputfilename, String outputfilename,
			int orientation, int compress_level, int quality) {
		try {
			recompressJPGFromFile(inputfilename, outputfilename, orientation,
					compress_level, quality);
			return 1;
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("ex:" + ex.getMessage());
			return 0;
		} catch (Error er) {
			er.printStackTrace();
			System.out.println("er" + er.getMessage());
			return 0;
		}
	}

	public int compressJPGFromBitmap(Bitmap bitmap, String outputfilename,
			int orientation, int compress_level) {
		try {
			recompressJPGFromBitmap(bitmap, outputfilename, orientation,
					compress_level);
			return 1;
		} catch (Exception ex) {
			ex.printStackTrace();
			return 0;
		} catch (Error er) {
			er.printStackTrace();
			return 0;
		}
	}

	/**
	 * Drawable转化为Bitmap
	 * 
	 * @author sunzheng
	 */
	public static Bitmap drawableToBitmap(Drawable drawable) {
		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
				.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, width, height);
		drawable.draw(canvas);
		return bitmap;
	}

	/**
	 * 转换图片成圆形
	 * 
	 * @param pic
	 *            传入Bitmap对象的字节
	 * @return
	 */
	public static Bitmap toRoundBitmap(byte[] pic, int picFormat) {
		return toRoundBitmap(getBitmapFromBytes(pic, picFormat));
	}

	/**
	 * 转换图片成圆形
	 * 
	 * @param bitmap
	 *            传入Bitmap对象
	 * @return
	 */
	public static Bitmap toRoundBitmap(Bitmap bitmap) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float roundPx;
		float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
		if (width <= height) {
			roundPx = width / 2;
			top = 0;
			bottom = width;
			left = 0;
			right = width;
			height = width;
			dst_left = 0;
			dst_top = 0;
			dst_right = width;
			dst_bottom = width;
		} else {
			roundPx = height / 2;
			float clip = (width - height) / 2;
			left = clip;
			right = width - clip;
			top = 0;
			bottom = height;
			width = height;
			dst_left = 0;
			dst_top = 0;
			dst_right = height;
			dst_bottom = height;
		}
		Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect src = new Rect((int) left, (int) top, (int) right,
				(int) bottom);
		final Rect dst = new Rect((int) dst_left, (int) dst_top,
				(int) dst_right, (int) dst_bottom);
		final RectF rectF = new RectF(dst);
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, src, dst, paint);
		return output;
	}

	/**
	 * 从view 得到图片
	 * 
	 * @param view
	 * @return
	 */
	public static Bitmap getBitmapFromView(View view) {
		return getBitmapFromView(view, 0, 0);
	}

	public static Bitmap getBitmapFromView(View view, int width, int height) {
		view.destroyDrawingCache();
		if (width * height > 0) {
			view.measure(View.MeasureSpec.makeMeasureSpec(0,
					View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
					.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
			view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
		} else {
			view.layout(0, 0, width, height);
		}
		view.setDrawingCacheEnabled(true);
		Bitmap bitmap = view.getDrawingCache(true);
		return bitmap;
	}

	public static Bitmap getBitmapFromBytes(byte[] pic, int picFormat) {
		if (null == pic) {
			return null;
		}
		return BitmapFactory.decodeByteArray(pic, 0, pic.length, null);

	}

	public static Bitmap ResizeBitmap(Bitmap bitmap, int width, int height) {
		if (width * height == 0) {
			return bitmap;
		}
		int bitmapWidth = bitmap.getWidth();
		int bitmapHeight = bitmap.getHeight();
		Bitmap thumb = Bitmap.createBitmap(width, width,
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(thumb);
		Matrix matrix = new Matrix();
		matrix.setScale(((float) width) / bitmapWidth, ((float) height)
				/ bitmapHeight);
		canvas.drawBitmap(bitmap, matrix, null);
		return thumb;
	}
}
